package com.plants.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plants.back.pojo.PlantsGoods;
import com.plants.back.pojo.PlantsManagent;
import com.plants.back.pojo.PlantsUser;
import com.plants.back.pojo.Vo;
import com.plants.back.service.impl.PlantsManagentServiceImpl;
import com.plants.back.service.impl.PlantsUserServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  用户控制器
 * </p>
 *
 * @author plants
 * @since 2024-08-06
 */
@CrossOrigin
@RestController
@RequestMapping("/plants_user")
public class PlantsUserController {

    @Resource
    private PlantsUserServiceImpl userService;

    // 注册
    @PostMapping("/register")
    public Vo<PlantsUser> register(@RequestBody PlantsUser ussr) {
        // 按照用户账号查询
        LambdaQueryWrapper<PlantsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(ussr.getAccount()), PlantsUser::getAccount, ussr.getAccount());
        PlantsUser one = userService.getOne(wrapper);
        // 判断用户是否存在
        if (one != null) {
            // 存在，不允许注册
            return new Vo<>(false, "该账号已存在，请更换一个账号");
        } else {
            // 不存在注册
            // 进行注册,添加到数据库
            boolean b = userService.save(ussr);
            if (b) {
                return new Vo<>(b, "注册成功，请进行登录！");
            } else {
                return new Vo<>(b, "注册失败，请稍后重试！");
            }
        }
    }
    // 登录
    @PostMapping("/login")
    public Vo<PlantsUser> login(@RequestBody PlantsUser user) {
        LambdaQueryWrapper<PlantsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(user.getAccount()), PlantsUser::getAccount, user.getAccount());
        wrapper.eq(StringUtils.isNotBlank(user.getPassword()), PlantsUser::getPassword, user.getPassword());
        PlantsUser one = userService.getOne(wrapper);
        if (one != null) {
            // 表示账号和密码正确，登录成功！
            return new Vo<>(true, one.getId());
        } else {
            // 表示密码或者账号不正确，需要提示用户重新输入！
            return new Vo<>(false, "账号或密码不正确，请重新输入！");
        }
    }
    // 分页查询
    @GetMapping("/page/{current}")
    public Vo<PlantsUser> page(@PathVariable("current") Integer current) {
        Page<PlantsUser> userPage = new Page<>(current <= 0 ? 1 : current, 6);
        IPage<PlantsUser> page = userService.page(userPage);
        if (page.getTotal() == 0) {
            return new Vo<>(false, "当前并没有用户！");
        }
        return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
    }
    // 搜索查询
    @GetMapping("/search/{name}/{current}")
    public Vo<PlantsUser> search(@PathVariable("name") String name, @PathVariable("current") Integer current) {
        Page<PlantsUser> userPage = new Page<>(current <= 0 ? 1 : current, 6);
        LambdaQueryWrapper<PlantsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PlantsUser::getName, name);
        Page<PlantsUser> page = userService.page(userPage, wrapper);
        if (page.getTotal() == 0) {
            // 表示没有数据
            return new Vo<>(true, "目前没有数据");
        } else {
            return new Vo<>(true, page.getRecords(), page.getCurrent(), page.getTotal());
        }
    }
    // 根据id修改用户信息
    @PostMapping("/update")
    public Vo<PlantsUser> update(@RequestBody PlantsUser user) {
        // 这个并不需要进行判断，可以直接进行信息的修改
        boolean b = userService.updateById(user);
        if (b) {
            return new Vo<>(b,"修改个人信息成功！");
        }else {
            return new Vo<>(b,"修改个人信息失败，请稍后重试！");
        }
    }
    // 删除用户
    @GetMapping("/delete/{id}")
    public Vo<PlantsUser> delete(@PathVariable("id") String id) {
        UpdateWrapper<PlantsUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        boolean b = userService.remove(wrapper);
        if (b) {
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }
}
